﻿using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;

namespace YawetagLib.Utilities
{
    public static class EmailHelper
    {
        public static void SendMessage(EmailOptions emailOptions, MailMessage mailMessage)
        {
            if (mailMessage.To.Count > 0)
            {
                SmtpClient smtpClient = new SmtpClient(emailOptions.SmtpHost, emailOptions.SmtpPort) { EnableSsl = emailOptions.EnableSsl };

                if (emailOptions.SmtpNeedsAuthentication)
                {
                    NetworkCredential networkCredential = new NetworkCredential(emailOptions.SmtpAuthenticationUserName, emailOptions.SmtpAuthenticationPassword);
                    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtpClient.Credentials = networkCredential;
                }

                CheckDebugModeForMailMessageTo(emailOptions, mailMessage);

                if (!emailOptions.Mode.Equals(EmailModeEnum.Off))
                {
                    smtpClient.Send(mailMessage);
                }
            }
        }

        public static void SendMessage(EmailOptions emailOptions, string subject, string mailFrom, string mailFromName,
            string[] mailTo, string body, bool isBodyHtml, params LinkedResource[] resources)
        {
            SendMessage(emailOptions, GetMailMessage(subject, mailFrom, mailFromName, mailTo, body, isBodyHtml, resources));
        }

        public static void SendMessage(EmailOptions emailOptions, string subject, string mailFrom, string mailFromName,
            string mailTo, string body, bool isBodyHtml, params LinkedResource[] resources)
        {
            SendMessage(emailOptions, GetMailMessage(subject, mailFrom, mailFromName, new[] { mailTo }, body, isBodyHtml, resources));
        }

        private static MailMessage GetMailMessage(string subject, string mailFrom, string mailFromName,
            IEnumerable<string> mailTo, string body, bool isBodyHtml, params LinkedResource[] resources)
        {
            MailMessage mailMessage = new MailMessage
            {
                From = new MailAddress(mailFrom, mailFromName),
                Subject = subject,
                IsBodyHtml = resources.Length > 0 || isBodyHtml
            };

            if (resources.Length > 0)
            {
                mailMessage.AlternateViews.Add(GetAlternateView(body, resources));
            }
            else
            {
                mailMessage.Body = body;
            }

            foreach (string email in mailTo)
            {
                mailMessage.To.Add(new MailAddress(email));
            }

            return mailMessage;
        }

        private static void CheckDebugModeForMailMessageTo(EmailOptions options, MailMessage mailMessage)
        {
            if (options.Mode.Equals(EmailModeEnum.Debug))
            {
                mailMessage.To.Clear();
                mailMessage.To.Add(new MailAddress(options.DebugPipeAddress));
            }
        }

        private static AlternateView GetAlternateView(string body, params LinkedResource[] resources)
        {
            AlternateView alternateView = AlternateView.CreateAlternateViewFromString(body,
                null, MediaTypeNames.Text.Html);

            foreach (LinkedResource resource in resources)
            {
                alternateView.LinkedResources.Add(resource);
            }

            return alternateView;
        }
    }
}
